package com.zb.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zb.entity.Car;
import com.zb.entity.Help;
import com.zb.entity.Mr;
import com.zb.entity.Store;
import com.zb.entity.Wx;
import com.zb.service.HelpService;
import com.zb.service.YhService;
import com.zb.serviceimpl.HelpServiceImpl;
import com.zb.serviceimpl.YhServiceImpl;
import com.zb.util.PageUtil;

/**
 * Servlet implementation class HelpServlet
 */
public class HelpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   

		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String op = request.getParameter("op");
		if(op.equals("show")) {
			this.doshow(request, response);
		}else if(op.equals("toupdate")) {
			this.toupadte(request, response);
		}else if(op.equals("doupdate")) {
			this.doupdate(request, response);
		}else if(op.equals("del")) {
			this.del(request, response);
		}
	}
	protected void doshow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HelpService h=new HelpServiceImpl();
		int index=1;
		String carnum=null;
		if (request.getParameter("index")!=null&&!"".equals(request.getParameter("index"))) {
			index = Integer.parseInt(request.getParameter("index"));
		}
		carnum=request.getParameter("carnum");
			PageUtil<Help> page = h.getHelpPage(index, carnum);
			request.setAttribute("mypage", page);
			
			
			request.getRequestDispatcher("search_jy.jsp").forward(request, response);
		
	}
	protected void toupadte(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HelpService helpServive=new HelpServiceImpl();
		int id = Integer.parseInt(request.getParameter("id"));
		
		Help p = helpServive.getHelpById(id);
		request.setAttribute("p",p);
		request.getRequestDispatcher("HelpUpdate.jsp").forward(request, response);
	}
	protected void doupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id=Integer.parseInt(request.getParameter("id"));
		String helpnum=request.getParameter("helpnum");
		String helpname=request.getParameter("helpname");
		String helpDate=request.getParameter("date");
		String helpmoney=request.getParameter("money");
		String helpshop=request.getParameter("helpshop");
		String remake=request.getParameter("remake");
		HelpService helpServive=new HelpServiceImpl();
		Help h=new Help();
		Car c=new Car();
		Store s=new Store();
		c.setId(id);
		h.setHelpNum(helpnum);
		h.setHelpName(helpname);
		h.setHelpDate(helpDate);
		h.setHelpMoney(helpmoney);
		h.setRemake(remake);
		h.setCar(c);
		helpServive.updateHelp(h);
		response.sendRedirect("Help.do?op=show");
	}
	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HelpService helpServive=new HelpServiceImpl();
		int nid= Integer.parseInt(request.getParameter("id"));
		
		helpServive.delHelp(nid);
		response.sendRedirect("Help.do?op=show");
	}
}
